//
//  KLWeekSuggestViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLWeekSuggestViewController.h"
#import "STTextView.h"
#define XInsetValeu 20
#define nomalHeight 44
@interface KLWeekSuggestViewController ()
@property(nonatomic, strong) STTextView                     *destextView;
@end

@implementation KLWeekSuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"指导";
    [self configSubView];
    [self configFooter];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIView * desView = [self headerViewWithTitle:@"指导意见:"];
    desView.top = 0;
    [header addSubview:desView];
    self.destextView = [[STTextView alloc] initWithFrame:CGRectMake(XInsetValeu, desView.bottom, UIScreenWidth - 2*XInsetValeu, 100)];
    self.destextView.placeholder = @"请输入您的指导意见(至少20字)";
    [self.destextView st_setBorderWith:1 borderColor:KL_lineColor cornerRadius:5];
    [header addSubview:self.destextView];
    
    header.height = self.destextView.bottom + 20;
    self.tableView.tableHeaderView = header;
}
- (void)configFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"提交"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:KL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    backView.backgroundColor = [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, 0, UIScreenWidth - 2* XInsetValeu, 44)
                                                     text:title
                                                textColor:KL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [backView addSubview:titleLable];
    return backView;
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    if (self.destextView.text.length < 20) {
        [SVProgressHUD showErrorWithStatus:@"指导意见最少20个字"];
        return;
    }
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (self.model) {
        [dic setObject:self.model.illness_id forKey:@"illness_id"];
    }
    if (self.destextView.text.length) {
        [dic setObject:self.destextView.text forKey:@"opinion"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/doctor/addopinion",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"指导成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                             }];
}
@end

